package com.trex.lwlsh.util;

import com.trex.lwlsh.model.CaseModel;
import com.trex.lwlsh.model.StepModel;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

import java.io.File;
import java.util.ArrayList;

/**
 * Create by Trex on 2018/1/13
 */
public class ReadExeclUtil {

    public static void main(String[] args) throws Exception {
        ReadExeclUtil rUtil=new ReadExeclUtil();
        rUtil.read("demo.xls");

    }


    /**
     * 模块下的所有case用例封装
     * @param Template
     * @return
     * @throws Exception
     */
    public ArrayList<CaseModel> read(String Template) throws Exception {
        Workbook wb = null;
        LogUtil.logs("【数据装箱】： FileName:"+ReadConfigUtil.readconfig("Templatepath")+Template);
        File FileSytem =new File(ReadConfigUtil.readconfig("Templatepath")+Template);
        wb = Workbook.getWorkbook(FileSytem);
        ArrayList<CaseModel> list = new ArrayList<CaseModel>();
        if (wb != null) {
            Sheet[] sheets = wb.getSheets();
            if (sheets != null && sheets.length != 0) {
                for (int i=0;i<sheets.length;i++) {
                    int rows = sheets[i].getRows();
                    for (int j=1;j<rows;j++) {
                        try{
                            Cell[] cells = sheets[i].getRow(j);
                            if(cells[3].toString().isEmpty()==false&&cells[2].toString().isEmpty()==false){
                                String CaseName=cells[0].getContents();
                                if(CaseName!=null&&CaseName.isEmpty()==false&&CaseName.length()>1){
                                    CaseModel cs=new  CaseModel();
                                    cs.setName(CaseName);
                                    cs.setLevel(cells[1].getContents());
                                    cs.setStepList(read(cells));
                                    //下面的for循环是一个用例多个步骤中获取每步步骤
                                    for(int k=j+1;k<rows;k++) {
                                        Cell[] cell = sheets[i].getRow(k);
                                        //获取第一列第i行的值如果是为空的就表示还是这一行的步骤是属于同一个用例
                                        if(cell[0].getContents()==null||cell[0].getContents().length()<1) {
                                            cs.setStepList(read(cell));
                                        }else{
                                            break;
                                        }
                                    }
                                    list.add(cs);
                                }
                            }
                        }catch ( Exception eo) {
                            eo.printStackTrace();
                        }
                    }
                }
            }

        }

        wb.close();
        return list;
    }

    /**
     * 步骤用例步骤
     * @param cells
     * @return
     * @throws Exception
     */
    public StepModel read(Cell[] cells) throws Exception {
        StepModel step = new StepModel();
        try{
            if (cells != null && cells.length != 0) {
                step.setName(cells[3].getContents());
                step.setMethod(cells[2].getContents());
                step.setUrl(cells[4].getContents());
                step.setParameter(cells[5].getContents());
                step.setTransfer(cells[6].getContents());
                step.setCheckStr(cells[7].getContents());
                step.setLevel(cells[1].getContents());
            }}catch(Exception e){
            e.printStackTrace();
        }
        return step;
    }

}
